package gerardoapps;

import java.util.ArrayList;
import java.util.List;

import sistemas.sam.R;




import android.app.Activity;
import android.content.Context;
import android.content.Intent;
import android.os.Bundle;
import android.view.LayoutInflater;
import android.view.View;
import android.view.View.OnClickListener;
import android.view.ViewGroup;
import android.widget.AdapterView;
import android.widget.AdapterView.OnItemClickListener;
import android.widget.AdapterView.OnItemSelectedListener;
import android.widget.ArrayAdapter;
import android.widget.ListView;
import android.widget.TextView;
import android.widget.Toast;

public class EmployeesListActivity extends Activity implements OnItemSelectedListener,OnItemClickListener {

	ListView listEmployeeDetails;
	public List<String> alumnos =new ArrayList<String>();
	public List<String> matricula =new ArrayList<String>();
	String nom = "",matri="";
	String idM,idG,idU,method,Matricula;
	@Override
	protected void onCreate(Bundle savedInstanceState) {
		super.onCreate(savedInstanceState);
		setContentView(R.layout.employeeslist);
		 
		Bundle bundle=getIntent().getExtras();
		
		
		idM=""+bundle.getString("idm");
		idG=""+bundle.getString("idg");
		idU=""+bundle.getString("idunidad");
	Matricula=""+bundle.getString("MatriculaP");
		
		ServerAccess ss=new ServerAccess();
		
		String c= ss.MatriaProfesorID(Matricula,idG);
		Toast.makeText(getApplicationContext(), ""+c, Toast.LENGTH_LONG).show();
		
		listEmployeeDetails=(ListView)findViewById(R.id.listViewEmployees);
		
		
		
		Employee[] employeesArray= ss.getEmployees(idU,idM,idG);
		
	
		
		EmployeeAdapter alumnoss=new EmployeeAdapter(this,R.layout.employeeitem, employeesArray);
		
		listEmployeeDetails.setAdapter(alumnoss);
		
	listEmployeeDetails.setOnItemClickListener(this);
		
		
		
		
		for(int i=0;i<alumnoss.array.length;i++){
			alumnos.add(""+alumnoss.array[i].nombre);
			matricula.add(""+alumnoss.array[i].matricula);
		}
		
		
	}

	private class EmployeeAdapter extends ArrayAdapter<Employee>
	{
		//Array to have the objects
		private Employee[] array;
		
		public EmployeeAdapter(Context context, int textViewResourceId,
				Employee[] objects) {
			super(context, textViewResourceId, objects);
			array=objects;
		}
		
		@Override
		public View getView(int position, View convertView, ViewGroup parent) {
			//Set the view for each item in the list view
			View v = convertView;
            if (v == null) {
                LayoutInflater vi = (LayoutInflater)getSystemService(Context.LAYOUT_INFLATER_SERVICE);
                v = vi.inflate(R.layout.employeeitem, null);
            }
            //Get the Textviews from the row view and set the appropriate values for them
            TextView labelName=(TextView) v.findViewById(R.id.labelName);
            TextView labelAddress=(TextView)v.findViewById(R.id.labelAddress);
            labelName.setText(array[position].nombre);
            labelAddress.setText(array[position].matricula);
			return v;
		}
	}

	@Override
	public void onItemSelected(AdapterView<?> arg0, View arg1, int location,
			long arg3) {
		// TODO Auto-generated method stub
		
	
			
			

	}

	@Override
	public void onNothingSelected(AdapterView<?> arg0) {
		// TODO Auto-generated method stub
		
	}

	@Override
	public void onItemClick(AdapterView<?> arg0, View arg1, int location, long arg3) {
		// TODO Auto-generated method stub
		nom=alumnos.get(location);
		matri=matricula.get(location);
		Toast.makeText(getApplicationContext(), nom+" "+matri, Toast.LENGTH_LONG).show();
		method="insert";
		
	Intent i=new Intent(this,Calificaciones_Alumn.class);
		Bundle bundle = new Bundle();
		bundle.putString("nombreA",nom);
		bundle.putString("matriA",matri);
		bundle.putString("IDm", idM);
        bundle.putString("IDu", idU);
        bundle.putString("IDg", idG); 
		bundle.putString("method", method);
		i.putExtras(bundle);
		startActivity(i);
	}


	

	
}
